package Datos;
import java.security.MessageDigest;
import java.sql.*;

public class Database {
	

	private String host = "jdbc:mysql://mysql-secretaria.jelastic.servint.net/secretaria";  
	private String user = "root";							 
	private String pass = "YyqXp9C0il";
	Connection conn;
	
	public Connection OpenConnection()
	{
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(host, user, pass);		
		}
		catch (ClassNotFoundException ex)
		{
			ex.getMessage();
		}
		catch (SQLException e2)
		{
			e2.getMessage();
		}
		
		return conn;
		
	}
	
	public void CloseConnection()
	{
		try {
			if (conn!=null)
			conn.close();
			} catch (SQLException e3) {
			//algo
			}	
	}
	
	 public String runSqlStatement(String cmd) {
	        
		 String msj = "";
		 try {
	            Statement statement = OpenConnection().createStatement();
	            statement.executeUpdate(cmd);
	           
	 
	        } catch (SQLException ex) {
	         msj += ex.getMessage();
	        
	        }
	        finally {CloseConnection();}
	        return msj;
	    }
	 
	 public ResultSet Select(String select)
	 {
		 ResultSet rs = null;
		 OpenConnection();
		 try{
		 Statement statement = conn.createStatement();
		 rs = statement.executeQuery(select);
		 }
		 catch(SQLException ex)
		 {
			 
		 }
		 catch(NullPointerException e)
		 {
			
		 }
		 return rs;
	 }
	 public String quotes(String string)
	 {
		 return "'"+string+"'";
	 }
	 public String quotes(int n)
	 {
		 return "'"+Integer.toString(n)+"'";
	 }
	 public String MD5(String md5) {
		   try {
		        MessageDigest md = MessageDigest.getInstance("MD5");
		        byte[] array = md.digest(md5.getBytes());
		        StringBuffer sb = new StringBuffer();
		        for (int i = 0; i < array.length; ++i) {
		          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
		       }
		        return sb.toString();
		    } catch (java.security.NoSuchAlgorithmException e) {
		    }
		    return null;
		}

}
